package com.shiyang.seckill.service;

import com.shiyang.seckill.dao.SeckillUserDao;
import com.shiyang.seckill.domain.SeckillUser;
import com.shiyang.seckill.exception.GlobalException;
import com.shiyang.seckill.redis.RedisService;
import com.shiyang.seckill.redis.SeckillUserKey;
import com.shiyang.seckill.result.CodeMsg;
import com.shiyang.seckill.util.MD5Util;
import com.shiyang.seckill.util.UUIDUtil;
import com.shiyang.seckill.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author shiyang
 * @create 2020-07-19 1:35 下午
 */
@Service
public class SeckillUserService {

    public static final String COOKIE_NAME_TOKEN = "token";

    @Autowired
    private SeckillUserDao seckillUserDao;
    @Autowired
    private RedisService redisService;

    public SeckillUser getById(long id) {
         return seckillUserDao.getById(id);
    }

    public SeckillUser getByToken(HttpServletResponse response, String token) {
        if (null == token) {
            return null;
        }

        SeckillUser user = redisService.get(SeckillUserKey.token, token, SeckillUser.class);
        if (null != user) {
            addCookie(response, token, user);
        }
        return user;
    }

    private void addCookie(HttpServletResponse response, String token, SeckillUser user) {
        redisService.set(SeckillUserKey.token, token, user);
        Cookie cookie = new Cookie(COOKIE_NAME_TOKEN, token);
        cookie.setMaxAge(SeckillUserKey.token.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public boolean login(HttpServletResponse response, LoginVo loginVo) {
        if(loginVo == null) {
            throw new GlobalException(CodeMsg.SERVER_ERR);
        }
        String mobile = loginVo.getMobile();
        String formPass = loginVo.getPassword();
        //判断手机号是否存在
        SeckillUser user = getById(Long.parseLong(mobile));
        if(user == null) {
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //验证密码
        String dbPass = user.getPassword();
        String saltDB = user.getSalt();
        String calcPass = MD5Util.formPassToDBPass(formPass, saltDB);
        if(!calcPass.equals(dbPass)) {
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }

        // 生成cookie
        String token = UUIDUtil.uuid();
//        redisService.set(SeckillUserKey.token, token, user);
//        Cookie cookie = new Cookie(COOKIE_NAME_TOKEN, token);
//        // 设置过期时间 和session一致 一定要设置过期时间否则session会不生效
//        cookie.setMaxAge(SeckillUserKey.token.expireSeconds());
//        // 设置项目根目录
//        cookie.setPath("/");
//        // 写回给客户端
//        response.addCookie(cookie);
        addCookie(response, token, user);

        return true;
    }

//    public CodeMsg login(LoginVo loginVo) {
//        if(loginVo == null) {
//            return CodeMsg.SERVER_ERR;
//        }
//        String mobile = loginVo.getMobile();
//        String formPass = loginVo.getPassword();
//        //判断手机号是否存在
//        SeckillUser user = getById(Long.parseLong(mobile));
//        if(user == null) {
//            return CodeMsg.MOBILE_NOT_EXIST;
//        }
//        //验证密码
//        String dbPass = user.getPassword();
//        String saltDB = user.getSalt();
//        String calcPass = MD5Util.formPassToDBPass(formPass, saltDB);
//        if(!calcPass.equals(dbPass)) {
//            return CodeMsg.PASSWORD_ERROR;
//        }
//
//        return CodeMsg.SUCCESS;
//    }
}
